<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <link rel="stylesheet" type="text/css" href="./css/percircle.css" />
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script>
    <script type="text/javascript" src="./script/jquery.min.js"></script>
</head>
<body>
    <header class="aui-bar aui-bar-nav">
        <a class="aui-pull-left" tapmode onclick="closeWin()">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title">修改密码</div>
        <a class="aui-pull-right" tapmode onclick="user_mobile_verification()">
            忘记密码
        </a>
    </header>
    <div class="aui-bar-nav-d"></div>

    <ul class="user-password">
        <li>
            <label>旧密码</label>
            <input class="text-1" type="text" placeholder="请输入旧的登录密码" id="old_pwd">
            <div class="check" id="checkOldPSW"></div>
        </li>
        <li>
            <label>新密码</label>
            <input class="text-1" type="text" placeholder="请输入新的登录密码" id="user_pwd">
            <div class="check" id="checkPSW"></div>
        </li>
        <li>
            <label>确认新密码</label>
            <input class="text-1" type="text" placeholder="请再次输入新的登录密码" id="user_pwd_confirm">
            <div class="check" id="checkPSW_2"></div>
        </li>
    </ul>

    <div class="user-data-btn" tapmode onclick="fixPwd();">确认修改</div>

</body>
<script type="text/javascript">

    function user_mobile_verification(){
        var delay = 0;
        if(api.systemType != 'ios'){
            // delay = 300;
        }
        api.openWin({
            name: 'user_mobile_verification',
            url: './user_mobile_verification.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false,
            progress:{
                type:"page"
            }
        });
    };
    
    function closeWin(){
        api.closeWin({
        });
    }

    // 确认修改
    function fixPwd(){

        // 旧密码
        var old_pwd = $api.byId('old_pwd');
        var old_pwdValue = old_pwd.value;
        // 新密码
        var user_pwd = $api.byId('user_pwd');
        var user_pwdValue = user_pwd.value;
        // 确认新密码
        var user_pwd_confirm = $api.byId('user_pwd_confirm');
        var user_pwd_confirmValue = user_pwd_confirm.value;

        // 旧密码不能为空
        if (!old_pwdValue) {
            $api.byId('checkOldPSW').innerText = '密码不能为空';
            return false;
        }else{
           $api.byId('checkOldPSW').innerText = ''; 
       }

        // 密码不能为空
        if (!user_pwdValue) {
            $api.byId('checkPSW').innerText = '密码不能为空';
            return false;
        }else{
           $api.byId('checkPSW').innerText = ''; 
       }

        // 再次输入密码不能为空
        if (!user_pwd_confirmValue) {
            $api.byId('checkPSW_2').innerText = '请再次输入密码';
            return false;
        }else{
            // 两次密码输入是否相同
            if (user_pwdValue != user_pwd_confirmValue) {
                $api.byId('checkPSW_2').innerText = '两次密码输入不一致';
                return false;
            }else{
                $api.byId('checkPSW_2').innerText = '';
            }
        }

        var userid = $api.getStorage('userid');

        api.ajax({
            url: webAddress + '/index.php?ctl=app_user_center2&act=edit_password',
            method: 'post',
            data: {
                values: { 
                    id: userid,
                    old_pwd:old_pwdValue,
                    new_pwd: user_pwdValue,
                    new_pwd2: user_pwd_confirmValue,
                },
            }
        },function(ret, err){
            if (ret) {
                if (ret.status == 0) {
                    alert(ret.info);
                    return false;
                }else{
                    alert('修改密码成功,请重新登陆');
                    $api.setStorage('userid', null);
                    // var closeToLogin = 'closeToLogin();';
                    // api.execScript({
                    //     name: 'home',
                    //     frameName: 'user',
                    //     script: closeToLogin,
                    // });
                    // api.closeToWin({
                    //     name: 'home'
                    // });

                    var setSele = 'randomSwitchBtn(2);';
                    api.execScript({
                        name: 'root',
                        script: setSele,
                    });
                    api.closeToWin({
                        name: 'root'
                    });
                }
            } else {
                alert(err.msg);
            }
        });
    }

</script>
</html>